package com.code.soulnetty.modbus.client.channel.impl;

import com.code.soulnetty.modbus.client.channel.ProtocolUtils;

import java.net.SocketAddress;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author lyf
 * @date 2022-12-17 15:40
 */
public class ProtocolChannelCache {
    private static final ConcurrentHashMap<String, ProtocolChannel> channels = new ConcurrentHashMap<>(32);

    private static ProtocolChannel get(String key) {
        return channels.get(key);
    }

    public static ProtocolChannel get(SocketAddress address) {
        return get(ProtocolUtils.protocolKey(address));
    }

    private static ProtocolChannel put(String key, ProtocolChannel channel) {
        return channels.put(key, channel);
    }

    public static ProtocolChannel put(ProtocolChannel channel) {
        return put(ProtocolUtils.protocolKey(channel.getRemoteAddress()), channel);
    }

    public static ProtocolChannel remove(SocketAddress address) {
        return channels.remove(ProtocolUtils.protocolKey(address));
    }
}
